﻿using MySqlX.XDevAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Client.HttpHelp
{
    public class Http : IDisposable
    {
        private readonly HttpClient httpClient;
        public Http()
        {
            httpClient = new HttpClient();
        }
        
        private string Uir_;
        public string Uir
        {
            get {


                if (string.IsNullOrWhiteSpace(Uir_))
                {
                    return HelpTools.IOC.Getobject<IConfiguration.IConfigurationHelp>().ReadNode("Uir");
                }
                else
                {
                    return Uir_;
                }          
            }
            set { Uir_ = value; 
           
            }
        }
        /// <summary>
        /// get请求
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetData(Dictionary<string,string> parameter)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Clear();
            stringBuilder.Append(stringBuilder);
            stringBuilder.Append("?");
            int i = 0;
            foreach (var item in parameter)
            {
                i++;
                if(parameter.Count==1)
                 stringBuilder.Append($"{item.Key}={item.Value}");
                else
                {
                    if(parameter.Count== i-1)
                        stringBuilder.Append($"{item.Key}={item.Value}");
                    else
                        stringBuilder.Append($"{item.Key}={item.Value}&");
                }
            }
            var data=  await  httpClient.GetAsync(stringBuilder.ToString());
            return await data.Content.ReadAsStringAsync();
        }
        /// <summary>
        /// get请求
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetData(string uir)
        {
            if (string.IsNullOrWhiteSpace(uir) && string.IsNullOrWhiteSpace(Uir))
            {
                return "请输入请求地址";
            }
            ServicePointManager.Expect100Continue = false;
            httpClient.Timeout = TimeSpan.FromSeconds(3);
            var data = await httpClient.GetAsync(string.IsNullOrWhiteSpace(uir)? Uir: uir);
            return await data.Content.ReadAsStringAsync();
        }
        /// <summary>
        /// post请求 T参数类型
        /// </summary>
        /// <returns></returns>
        public async Task<string> PostData<T>(T parameter,string uir_,string mediaType)
        {
            var data = await httpClient.PostAsync(string.IsNullOrWhiteSpace(uir_)? $"{Uir}": $"{Uir}/{uir_}", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(parameter), Encoding.UTF8, mediaType));
            return await data.Content.ReadAsStringAsync();
        }

        public void Dispose()
        {
            httpClient.Dispose();
        }
    }
}
